package exp.parser;

import grammar.parser.FuncFParams;
import word.parser.WordElement;

import java.util.ArrayList;
import java.util.List;

public class EqExp extends ExpParser {
    private final ExpParser left;
    private final ExpParser right;
    private final boolean isEqual;

    public EqExp(ExpParser left, ExpParser right, boolean isEqual) {
        super(false, left.isConst() && (right == null || right.isConst()));
        this.left = left;
        this.right = right;
        this.isEqual = isEqual;
    }

    public static EqExp parseEqExp() {
        List<ExpParser> expParserList = new ArrayList<>();
        List<Boolean> equalFlagList = new ArrayList<>();
        expParserList.add(RelExp.parserRelExp());
        while (getAnalyzingWord().getType() == WordElement.WordType.EQL ||
                getAnalyzingWord().getType() == WordElement.WordType.NEQ) {
            equalFlagList.add(getAnalyzingWord().getType() == WordElement.WordType.EQL);
            analyzeNextWord();
            expParserList.add(RelExp.parserRelExp());
        }
        equalFlagList.add(true);
        ExpParser left = expParserList.get(0);
        for (int i = 1; i <= expParserList.size(); i++) {
            ExpParser right = i < expParserList.size() ? expParserList.get(i) : null;
            left = new EqExp(left, right, equalFlagList.get(i - 1));
        }
        return (EqExp) left;
    }

    @Override
    public LVal toLVal() {
        return null;
    }

    @Override
    public Integer compute() {
        int l = left.getValue();
        if (right == null) {
            return l;
        }
        int r = right.getValue();
        return isEqual ?
                l == r ? 1 : 0 :
                l != r ? 1 : 0;
    }

    @Override
    public FuncFParams.FuncFParam checkDimension() {
        return new FuncFParams.FuncFParam();
    }
}
